// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const TcbRouter = require('tcb-router')

const db = cloud.database()

const plCollection = db.collection('tiezi')
const plCollection2 = db.collection('tupian')
const plCollection3 = db.collection('shipin')
const plCollection4 = db.collection('tuijian')
const zhuantiCollection = db.collection('zhuanti')

// 云函数入口函数
exports.main = async(event, context) => {
  const app = new TcbRouter({
    event
  })
  let biao=parseInt(event.biao)
  app.router('lost', async(ctx, next) => {

    let zhuantiList = await zhuantiCollection.skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
    ctx.body = zhuantiList
  })
  // 活动
  app.router('get', async(ctx, next) => {
    if(biao==0){
          ctx.body = await plCollection4.where({
        id:event.id
      }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
    }else if(biao==111){
      ctx.body = await plCollection2.where({
        id:event.id
      }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
    }else if(biao==222){
      ctx.body = await plCollection.where({
        id:event.id
      }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
    }else if(biao==333){
      ctx.body = await plCollection3.where({
        id:event.id
      }).skip(event.start).limit(event.count)
      .orderBy('createTime', 'desc').get().then((res) => {
        return res.data
      })
    }

  })


  return app.serve()
}